package com.neuedu.common.core.domain;

import com.neuedu.common.core.domain.enums.ResultEnum;

//import java.io.Serial;
import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author Punkhoo
 */
@Setter
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {

//    @Serial
    private static final long serialVersionUID = 1L;

    protected Boolean isSuccess;

    private int code;

    private String msg;

    private T data;

    public static <T> R<T> success() {
        return new R<>(Boolean.TRUE, ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), null);
    }

    public static <T> R<T> success(T data) {
        return new R<>(Boolean.TRUE, ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMessage(), data);
    }

    public static <T> R<T> success(String message, T data) {
        return new R<>(Boolean.TRUE, ResultEnum.SUCCESS.getCode(), message, data);
    }

    public R<T> setSuccess(T data) {
        return this.setIsSuccess(Boolean.TRUE).setCode(ResultEnum.SUCCESS.getCode()).setMsg(ResultEnum.SUCCESS.getMessage()).setData(data);
    }

    public static <T> R<T> fail() {
        return new R<>(Boolean.FALSE, ResultEnum.FAILED.getCode(), ResultEnum.FAILED.getMessage(), null);
    }

    public static <T> R<T> fail(String message) {
        return new R<>(Boolean.FALSE, ResultEnum.FAILED.getCode(), message, null);
    }

    public R<T> setFail(String message) {
        return this.setIsSuccess(Boolean.FALSE).setCode(ResultEnum.FAILED.getCode()).setMsg(message).setData(null);
    }

    public boolean isSuccess(){
        return this.getIsSuccess();
    }

}
